﻿using DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.CRUD;

namespace TrainTicketBookingSystem
{
    internal class OrderService
    {
        public void AddOrder(Order o)
        {
            string sql = "insert `order` (`userId`,`seatId`,`trainSequenceId`) " +
                " VALUES(@userId,@seatId,@trainSequenceId); ";

            MySqlUtil.ExecutQuery(sql,
                new MySqlParameter("@userId",o.UserId),
                new MySqlParameter("@seatId",o.SeatId),
                new MySqlParameter("@trainSequenceId",o.TrainSequenceId));
        }

        public List<Order> FindOrderByAccount(string Account)
        {
            UserService userService = new UserService();
            List<Order> OrdersList = new List<Order>();
            User u = userService.FindUserByAccount(Account);

            string sql = " select * from `order` where userId = @userId ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql,new MySqlParameter("@userid",u.Id));

            while (reader.Read())
            {
                int OrderId = reader.GetInt32("orderId");
                int UserId = reader.GetInt32("userId");
                int SeatId = reader.GetInt32("seatId");
                int TrainSequenceId = reader.GetInt32("trainSequenceId");
                DateTime OrderTime = reader.GetDateTime("orderTime");

                OrdersList.Add(new Order(OrderId,UserId,SeatId,TrainSequenceId,OrderTime));
            }

            return OrdersList;
        }

        public void DeleteOrderById(int id)
        {
            string sql = " delete from `order` where orderId = @orderId ";
            MySqlUtil.ExecutQuery(sql,new MySqlParameter("@orderId", id));
        }
    }
}
